﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageContracts =Skynet.ServiceContracts.MessageContracts;
using BusinessComponent = Skynet.BusinessLogic.BusinessComponents;

using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Skynet.BusinessEntities;
using Skynet.ServiceContracts.MessageContracts;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Security.Principal;
using System.ServiceModel.Dispatcher;
using Skynet.ServiceContracts.FaultContracts;
using Infrastructure.Aspect.ExceptionHandling;
namespace Skynet.ServiceImplementation
{
    [AspNetCompatibilityRequirements (RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
    [ExceptionShielding("ServiceInterfacePolicy")]
    public class SkynetLibraryService : ServiceContracts.ISkynetLibraryService
    {
        [Dependency]
        public BusinessComponent.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService EntityTranslatorService { get; set; }                      
        
        [Dependency]
        public IUnityContainer UnityContainer { protected get; set; }

        public SkynetLibraryService()
        {
        
        }   
        //[OperationBehavior(Impersonation= ImpersonationOption.Required)]
        public virtual string GetStatus()
        {
            try
            {                

                var defaultWriter = UnityContainer.Resolve<LogWriter>();
                if (defaultWriter.IsLoggingEnabled())
                {
                    defaultWriter.Write(string.Format("Windows Identity {0}", WindowsIdentity.GetCurrent().Name), Infrastructure.Aspect.Constants.LoggingPolicyNames.Infrasstructure);
                }                
            }
            catch (Exception ex)
            {
                var fault = new ServiceContracts.FaultContracts.SkynetServiceFault();
                fault.UserMessage = "Service Error";
                fault.SupportMessage = ex.Message;
                fault.ErrorCode = "UIINPUT";


                throw new FaultException<SkynetServiceFault>(fault, "Skynet service Error");
            }
            return "server running";            
        }

        public virtual void LogService(string Message)
        {
            Console.WriteLine(Message);
            //throw new ApplicationException("error en la consola");
        }
       // [System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Role="Developers")]
        public virtual MessageContracts.FindCategoryResponse FindExamCategories(MessageContracts.FindCategoryRequest request)
        {

          
            var response = new MessageContracts.FindCategoryResponse();
            
            switch (request.Operation)
            {                
                case Skynet.ServiceContracts.DataContracts.DCExamFindCategoryOperation.FindById:
                    var id = System.Diagnostics.Trace.CorrelationManager.ActivityId;                    
                    response.ExamCategories = new List<ServiceContracts.DataContracts.DCExamCategory>();
                    break;
                case ServiceContracts.DataContracts.DCExamFindCategoryOperation.FindAll:
                    response.ExamCategories = EntityTranslatorService.Translate<IEnumerable<ServiceContracts.DataContracts.DCExamCategory>>(this.BCExamQueries.SearchExamCategories());
                    break;
                default:
                    break;
            }            
            return response;
        }


        public virtual MessageContracts.UpdateExamCategoryResponse UpdateExamCategory(MessageContracts.UpdateExamCategoryRequest request)
        {
            var response = new  UpdateExamCategoryResponse();            
            this.BCExams.MaintenanceExamCategories(request.Categories);
            return response;            
        }

        public virtual MessageContracts.MaintenanceQuestionsResponse MaintenanceQuestion(MessageContracts.MaintenanceQuestionsRequest request)
        {
            var response = new MaintenanceQuestionsResponse();



            return response;
        }


        public virtual MaintenanceExamCategoryResponse MaintenanceExamCategory(MaintenanceExamCategoryRequest request)
        {
            throw new NotImplementedException();
        }
    }
}
